var xmlHttp;
var xmlDoc;

xmlHttp=GetXmlHttp();
if (xmlHttp==null)
{
	alert ("Seu browser não suporta AJAX!");
} 



xmlHttp.open("GET","petshop.xml",false); //abre objeto xmlHttp com url
xmlHttp.send(null);// envia requisicao
xmlDoc = xmlHttp.responseXML; //cria documento (conteudo do xml)
var elementos = getElements(localStorage.globalTipo);

var contName = new Array();
var conteudo = new Array();
var count;
var elemento;


if(localStorage.globalTipo == "donos")
{
	for(j = 0; j < elementos.length; ++j)
	{
		if(elementos[j].getElementsByTagName("userid")[0].firstChild.nodeValue == localStorage.ID)
		{
			elemento = elementos[j];
			break;
		}
	}
	contName[0] = "Nome";
	conteudo[0] = elemento.getElementsByTagName("nome")[0].firstChild.nodeValue;
	contName[1] = "Sobrenome";
	conteudo[1] = elemento.getElementsByTagName("sobrenome")[0].firstChild.nodeValue;
	contName[2] = "ID";
	conteudo[2] = elemento.getElementsByTagName("userid")[0].firstChild.nodeValue;
	contName[3] = "Sexo";
	conteudo[3] = elemento.getElementsByTagName("sexo")[0].firstChild.nodeValue;
	contName[4] = "Data de Nascimento";
	conteudo[4] = elemento.getElementsByTagName("birthday")[0].firstChild.nodeValue;
	contName[5] = "Email";
	conteudo[5] = elemento.getElementsByTagName("email")[0].firstChild.nodeValue;
	contName[6] = "Tel";
	conteudo[6] = elemento.getElementsByTagName("tel")[0].firstChild.nodeValue;
	contName[7] = "Cel";
	var cel = elemento.getElementsByTagName("cel")[0];
	if(cel != null)
		conteudo[7] = cel.firstChild.nodeValue;
	else
		conteudo[7] = "Nao possui";
	var endereco = elemento.getElementsByTagName("endereco")[0];
	
	contName[8] = "Endereco";
	conteudo[8] = endereco.getElementsByTagName("nomerua")[0].firstChild.nodeValue+", "+
	endereco.getElementsByTagName("num")[0].firstChild.nodeValue+" - "+
	endereco.getElementsByTagName("bairro")[0].firstChild.nodeValue+" - "+
	endereco.getElementsByTagName("cidade")[0].firstChild.nodeValue+" - "+
	endereco.getElementsByTagName("estado")[0].firstChild.nodeValue;
	
	count = 9;
}
else if(localStorage.globalTipo == "animais")
{
	for(j = 0; j < elementos.length; ++j)
	{
		if(elementos[j].getElementsByTagName("bixoid")[0].firstChild.nodeValue == localStorage.ID)
		{
			elemento = elementos[j];
			break;
		}
	}
	contName[0] = "Nome";
	conteudo[0] = elemento.getElementsByTagName("nome")[0].firstChild.nodeValue;
	contName[1] = "ID";
	conteudo[1] = elemento.getElementsByTagName("bixoid")[0].firstChild.nodeValue;	
	contName[2] = "Raca";
	conteudo[2] = elemento.getElementsByTagName("raca")[0].firstChild.nodeValue;	
	contName[3] = "Sexo";
	conteudo[3] = elemento.getElementsByTagName("anisx")[0].firstChild.nodeValue;
	contName[4] = "Especie";
	conteudo[4] = elemento.getElementsByTagName("tipo")[0].firstChild.nodeValue;	
	contName[5] = "ID do dono";
	conteudo[5] = elemento.getElementsByTagName("donoid")[0].firstChild.nodeValue;	
	count = 6;		
}
else if(localStorage.globalTipo == "produtos")
{

	for(j = 0; j < elementos.length; ++j)
	{
		if(elementos[j].getElementsByTagName("pcod")[0].firstChild.nodeValue == localStorage.ID)
		{
			elemento = elementos[j];
			break;
		}
	}	
	contName[0] = "Nome do produto";
	conteudo[0] = elemento.getElementsByTagName("nome")[0].firstChild.nodeValue;
	contName[1] = "Codigo";
	conteudo[1] = elemento.getElementsByTagName("pcod")[0].firstChild.nodeValue;	
	contName[2] = "Preco";
	conteudo[2] = "R$"+elemento.getElementsByTagName("pproduto")[0].firstChild.nodeValue;	
	count = 3;		
}


var divContent = "<table>";
for(i = 0; i < count; ++i)
{
	divContent = divContent + "<tr>" + "<td>" + contName[i]+ ":</td> ";
	divContent = divContent + "<td>" + conteudo[i] + "</td> ";
	divContent = divContent + "</tr>";

}
divContent = divContent + "</table>";
var div = "<div class=\"content\">"+divContent+"</div>";
var doc = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /></head>" +
"<body>"+div+"</body> </html>";
document.write("<h1>Detalhes</h1>");
document.write(doc);


function getElements(tipo)
{
	var elements;
	var tagName;
	if(tipo == "produtos")
	{
		tagName = "prod";
	}
	else if(tipo == "donos")
	{
		tagName = "pessoa";
	}
	else if(tipo == "animais")
	{
		
		tagName = "animal";
	}
	elements =  xmlDoc.getElementsByTagName(tagName);
	return elements;
}

function GetXmlHttp()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Safari, Opera
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer 7.0-
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	
	}
	return xmlHttp;
}

localStorage.clear();